import { defineStore } from "pinia";

interface ITab{
    title: string,
    path: string,
    isActive: boolean
}

const useTabsStore = defineStore("tabs", {
    state(){
        return {
            tabs:[
                {
                    title: '总览',
                    path: '/totalview',
                    isActive: false
                },
            ]
        };
    },
    actions:{
        // 添加当前访问的tab记录
        addTab(tab:ITab){
            const isExist = this.tabs.some(item => item.path === tab.path);
            if(!isExist){
                this.tabs.push(tab);
            }
        },
        removeTab(tab:ITab){
            this.tabs = this.tabs.filter(item => item.path !== tab.path);
        },
        removeByCommand(command:string){
            switch(command){
                case 'current': // 关闭当前标签页
                    this.tabs = this.tabs.filter(tab=>!tab.isActive);
                    break;
                case 'other': // 关闭其他标签页
                    this.tabs = this.tabs.filter(tab=>tab.isActive);
            }
        },
    }
})

export default useTabsStore